package cn.net.gemai.springboot_swagger.config;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 配置Swagger info 信息
     */
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())
                .enable(true)//是否启用swagger false不启用
                .groupName("group001")
                .select()
                /**
                 * 配置RequestHandlerSelectors 扫描接口的方式
                 * 指定扫描的包basePackage
                 * any()扫描全部
                 * none()不扫描
                 * withClassAnnotation：扫描类上的注解
                 */
                .apis(RequestHandlerSelectors.any())
                .paths(Predicates.not(PathSelectors.regex("/error.*")))//错误路径不监控
                .paths(PathSelectors.regex("/.*"))// 对根下所有路径进行监控
                .build();
    }

    public ApiInfo getApiInfo(){
//        Contact contact = new Contact("smalltrees","https://blog.csdn.net/small_trees","oneworlder@outlook.com");
        return new ApiInfoBuilder()
                .title("API Documents")
                .description("此项目用来帮助更多的人入门Springboot，模块组件拿来即用。")
//                .termsOfServiceUrl("https://gitee.com/smalltrees/SpringBootDemo")
//                .contact(contact)
                .license("GPL-2.0")
                .licenseUrl("https://gitee.com/smalltrees/SpringBootDemo/blob/master/LICENSE")
                .version("1.0.0")
                .build();
    }


    /**
     * 可以配置多个Docket分组
     */
    @Bean
    public Docket docket02(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo02())
                .enable(true)
                .groupName("group002");
    }

    public ApiInfo getApiInfo02(){
        return new ApiInfo("我的项目的Api文档002",
                "此项目用来帮助更多的人入门Springboot，模块组件拿来即用。",
                "1.0.0.4",
                "https://gitee.com/smalltrees/SpringBootDemo",
                null, "GPL-2.0",
                "https://gitee.com/smalltrees/SpringBootDemo/blob/master/LICENSE",
                new ArrayList());
    }

    @Bean
    public Docket docket03(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo03())
                .enable(true)
                .groupName("group003");
    }

    public ApiInfo getApiInfo03(){
        return new ApiInfo("我的项目的Api文档003",
                "此项目用来帮助更多的人入门Springboot，模块组件拿来即用。",
                "1.0.1.5",
                "https://gitee.com/smalltrees/SpringBootDemo",
                null, "GPL-2.0",
                "https://gitee.com/smalltrees/SpringBootDemo/blob/master/LICENSE",
                new ArrayList());
    }
}
